/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.ledger.account.dao.hibernate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.redragon.framework.annotation.Cache;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import com.erp.ledger.account.dao.GLLedgerDao;
import com.erp.ledger.account.dao.model.GLLedger;
import com.erp.ledger.account.dao.model.GLLedgerCO;
import com.erp.masterdata.calendar.dao.model.MdFinanceCalendar;

@Repository
public class GLLedgerDaoImpl implements GLLedgerDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(GLLedger obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(GLLedger obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(GLLedger obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(GLLedger obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<GLLedger> getDataObjects() {
        return this.daoSupport.getDataAllObject(GLLedger.class);
    }

    @Override
    public GLLedger getDataObject(int id) {
        return (GLLedger)this.daoSupport.getDataObject(GLLedger.class, id);
    }
    
    @Override
    public GLLedger getDataObject(String code) {
        String sql = "select c.* from gl_ledger c where c.ledger_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", GLLedger.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return (GLLedger)list.get(0);
        }
        
        return null;
    }
    
    @Override
    public List<GLLedger> getDataObjects(GLLedgerCO paramObj) {
        String sql = "select c.* from gl_ledger c where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "approveStatus", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and c.", args);
        sql = sql + " order by c.ledger_id desc";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", GLLedger.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public List<GLLedger> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<GLLedger> getDataObjects(Pages pages, GLLedgerCO paramObj) {
        String sql = "select c.* from gl_ledger c where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "ledgerCode", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "ledgerName", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and c.", args);
        sql = sql + " order by c.ledger_id desc";
        
        this.daoSupport.validateDataObject(paramObj);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", GLLedger.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, GLLedgerCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<GLLedger> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, GLLedgerCO paramObj) {
        String sql = "select c.* from gl_ledger c where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "ledgerCode", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "ledgerName", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and c.", args);
        sql = sql + DaoCRUDUtil.getDataAuthSQL(dataAuthSQL, "c.", "c.");
        sql = sql + " order by c.ledger_id desc";
        
        this.daoSupport.validateDataObject(paramObj);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", GLLedger.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }
    
    @Override
    public void updateApproveStatus(String code, String approveStatus) {
        String sql = "update gl_ledger set approve_status = :approveStatus";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        args.put("approveStatus", approveStatus);
        
        if(approveStatus.equals("UNSUBMIT")) {
            sql = sql + " ,version = version+1";
        }
        
        sql = sql + " where ledger_code = :code";
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public GLLedger getPrimaryGLLedgerByLegal(String legalCode) {
        String sql = "select g.* from gl_ledger g where g.legal_code = :legalCode and g.primary_flag = 'Y'";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("legalCode", legalCode);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("g", GLLedger.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return (GLLedger)list.get(0);
        }
        
        return null;
    }
    
    @Override
    public int getSegmentNumByLegal(String legalCode, String segmentType) {
        String sql = "select g.* from gl_ledger g where g.legal_code = :legalCode and g.primary_flag = 'Y'";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("legalCode", legalCode);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("g", GLLedger.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            GLLedger glLedger = (GLLedger)list.get(0);
            if(glLedger.getSegment1().contentEquals(segmentType)) {
                return 1;
            }else if(glLedger.getSegment2().contentEquals(segmentType)) {
                return 2;
            }else if(glLedger.getSegment3().contentEquals(segmentType)) {
                return 3;
            }else if(glLedger.getSegment4().contentEquals(segmentType)) {
                return 4;
            }else if(glLedger.getSegment5().contentEquals(segmentType)) {
                return 5;
            }else if(glLedger.getSegment6().contentEquals(segmentType)) {
                return 6;
            }else if(glLedger.getSegment7().contentEquals(segmentType)) {
                return 7;
            }else if(glLedger.getSegment8().contentEquals(segmentType)) {
                return 8;
            }else if(glLedger.getSegment9().contentEquals(segmentType)) {
                return 9;
            }else if(glLedger.getSegment10().contentEquals(segmentType)) {
                return 10;
            }else if(glLedger.getSegment11().contentEquals(segmentType)) {
                return 11;
            }else if(glLedger.getSegment12().contentEquals(segmentType)) {
                return 12;
            }else if(glLedger.getSegment13().contentEquals(segmentType)) {
                return 13;
            }else if(glLedger.getSegment14().contentEquals(segmentType)) {
                return 14;
            }else if(glLedger.getSegment15().contentEquals(segmentType)) {
                return 15;
            }
        }
        
        return 0;
    }
    
}
